package com.li.shop.service.Impl;

import com.li.shop.domain.Admin;
import com.li.shop.domain.AdminExample;
import com.li.shop.mapper.AdminMapper;
import com.li.shop.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminmapper;

    @Override
    public void add(Admin admin) {
        adminmapper.insert(admin);
    }

    @Override
    public void delete(String email) {
        adminmapper.deleteByPrimaryKey(email);
    }

    @Override
    public void update(Admin admin) {
        adminmapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public Admin login(String email, String password) {
        AdminExample example=new AdminExample();
        example.createCriteria().andEmailEqualTo(email).andPasswordEqualTo(password);
        List<Admin> admins=adminmapper.selectByExample(example);
        if(admins.isEmpty())
        {
            return null;
        }
        return admins.get(0);
    }

    @Override
    public Admin get(String email) {
        return adminmapper.selectByPrimaryKey(email);
    }

    @Override
    public List<Admin> list() {
        AdminExample example = new AdminExample();
        example.setOrderByClause("id desc");
        List<Admin> admins = adminmapper.selectByExample(example);
        return admins;
    }

    @Override
    public Admin get(String email, String password) {
        AdminExample example = new AdminExample();
        example.createCriteria().andNameEqualTo(email).andPasswordEqualTo(password);
        List<Admin> admins = adminmapper.selectByExample(example);
        if(admins.isEmpty())
            return null;
        return admins.get(0);
    }

    @Override
    public boolean exist(String email) {
        AdminExample example = new AdminExample();
        example.createCriteria().andNameEqualTo(email);
        List<Admin> admins = adminmapper.selectByExample(example);
        if(admins.isEmpty()){
            return false;
        }
        return true;
    }

    @Override
    public void register(Admin admin) {

    }

    @Override
    public Admin checkCode(String code) {
        return null;
    }

    @Override
    public void updateAdminStatus(Admin admin) {

    }

    @Override
    public Admin loginAdmin(Admin admin) {
        return null;
    }
}
